#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common_log.h"
#include "curl_package.h"
#include "directory.h"
#include "ftpget_conf.h"
#include "ftpget_rule.h"
#include "record_cache.h"
#include "transcode.h"

typedef struct {
	t_FtpRule *pFtpRule;
	t_WildSection *pWildSection;
	t_RecordCache *pRecCacheIn;
	char *pRemotePath;
	char *pSaveName;
} t_FtpRemainData;

static int FtpFilter(struct curl_fileinfo *finfo, int remains) {
	char *gbkname = (char *)malloc(256);
	UTF8ToGBK(finfo->filename, gbkname, 256);
	printf("%3d %10luB %s %s\n", remains, (unsigned long)finfo->size, finfo->b_data, gbkname);
	free(gbkname);
	return 1;
}

static int FtpCombPath(struct curl_fileinfo *finfo, const char *ldir, const void *remaindata, char *lpath, int lsize) {
	const t_FtpRemainData *ftpRemainData = (t_FtpRemainData *)remaindata;	
	const t_FtpRule *ftpRule = ftpRemainData->pFtpRule;
	t_WildSection remoteWildSection;
	char localName[256], localPath[256], nowTime[32];
	char remotePath[256], remotePathWild[256];	
	char *gbkname = (char *)malloc(256);
	int intervalCache, interval, result = -1;
	UTF8ToGBK(finfo->filename, gbkname, 256);
	strcpy(remotePath, ftpRemainData->pRemotePath);
	remotePath[strlen(ftpRemainData->pRemotePath)-1] = 0;
	strcat(remotePath, gbkname);
	sprintf(remotePathWild, "ftp://%s:%s@%s:%d/%s/%s", ftpRule->username, ftpRule->passwd, 
		ftpRule->ip, ftpRule->port, ftpRule->dir, ftpRule->name);
	FtpRule_SetWildSection(remotePath, remotePathWild, &remoteWildSection);
	sprintf(nowTime, "%s%s%s%s%s%s", remoteWildSection.year4, remoteWildSection.month2, remoteWildSection.day2,
		remoteWildSection.hour242, remoteWildSection.minute2, remoteWildSection.second2);
	interval = RCache_GetInterval(ftpRemainData->pRecCacheIn->time, nowTime);
	intervalCache = atoi(ftpRemainData->pRecCacheIn->interval);
	if (0 <= interval && intervalCache > interval) {
		FtpRule_ParseWildPath(localName, sizeof(localName), &remoteWildSection, ftpRemainData->pSaveName);
		if (NULL == ldir)
			strcpy(lpath, localName);
		else {
			sprintf(lpath, "%s\\%s", ldir, localName);
		}	
		result = 1;
	}
	free(gbkname);
	return result;
}

#if 1
int main(void) {
	char workDir[256], localDir[256], saveName[256];
	char remotePath[256], wildPath[256];
	char rulePath[256], cachePath[256], confPath[256];
	t_FtpRuleArray ftpRuleArray;
	t_RecordCache recCacheIn, recCacheOut;
	t_FtpRule *ftpRule = NULL;
	t_WildSection wildSection;
	t_FtpRemainData ftpRemainData;
	int fIdx;
	bool okDown;

	GetWorkDir(workDir, sizeof(workDir));
	sprintf(rulePath, "%s\\ftpget.rule", workDir);
	sprintf(cachePath, "%s\\ftpget.cache", workDir);
	sprintf(confPath, "%s\\ftpget.ini", workDir);

	Conf_ReadSaveDir(confPath, localDir, sizeof(localDir));
	Conf_ReadSaveName(confPath, saveName, sizeof(saveName));
	CheckDirectory(localDir);	
	FtpRule_Read(rulePath, &ftpRuleArray);
	FtpRule_Print(&ftpRuleArray);
	Curl_Init();
	while (1) {
		Sleep(10);
		RCache_Read(cachePath, &recCacheIn);
		RCache_Print(&recCacheIn);
		for (fIdx = 0; fIdx < ftpRuleArray.ftprule_num; fIdx++) {
			ftpRule = &ftpRuleArray.ftprule[fIdx];
			if (0 >= ftpRule->usedflag) {
				continue;
			} 
			memset(&wildSection, 0, sizeof(t_WildSection));
			FtpRule_SetWildTime(recCacheIn.time, "%Y%m%d%H%M%S", &wildSection);
			strcpy(wildSection.location, ftpRule->location);
			sprintf(wildSection.lane1, "%01d", ftpRule->lane);
			sprintf(wildSection.lane2, "%02d", ftpRule->lane);
			sprintf(wildPath, "ftp://%s:%s@%s:%d/%s/*", ftpRule->username, ftpRule->passwd, ftpRule->ip, ftpRule->port, ftpRule->dir);
			FtpRule_ParseWildPath(remotePath, sizeof(remotePath), &wildSection, wildPath);	
			ftpRemainData.pFtpRule = ftpRule;
			ftpRemainData.pWildSection = &wildSection;
			ftpRemainData.pRecCacheIn = &recCacheIn;
			ftpRemainData.pRemotePath = remotePath;
			ftpRemainData.pSaveName = saveName;
			okDown = Curl_DownloadWildcard(remotePath, localDir, FtpFilter, FtpCombPath, 3000, &ftpRemainData);
		}
		RCache_AddInterval(recCacheIn.time, recCacheIn.interval, recCacheOut.time, sizeof(recCacheOut.time));
		strcpy(recCacheOut.interval, recCacheIn.interval);
		RCache_Write(cachePath, &recCacheOut);
	}
	Curl_Uninit();

	return 0;
}
#endif

